#
#   run this script before your script, will automatically set 
#
#   PYTHON_VERSION          the version of python, like 36
#   PYTHON_PREFIX           the install directory of python
#   PYTHON_INCLUDE_PATH     the include directory of python where you can find Python.h
#   PYTHON_LIB_PATH         the lib directory of python where you can find pythonXX.lib
#   PYTHON_LIB              the python lib name, needed in windows platform. like pythonXX.lib
#   PYTHON_EXT_SUFFIX       the suffix extension, like .pyd .so .cpythonXXm-darwin.so
#   PYTHON_SITE_PATH        the site-package directory where you install the python module
#
#   variables for you according to the python which you use in your system environement.
#

cmake_minimum_required(VERSION 3.10)

#
execute_process( COMMAND "python" "${PROJECT_SOURCE_DIR}/PyBind/PythonPath.py"
    RESULT_VARIABLE _CMD_SUCCESS
    OUTPUT_VARIABLE _CMD_VALUES
    ERROR_VARIABLE _CMD_ERROR   )

if(NOT _CMD_SUCCESS MATCHES 0)
    message(FATAL_ERROR ${_CMD_ERROR})
    return()
endif()

# Convert the process output into a list
string(REGEX REPLACE "\n" ";" _CMD_VALUES ${_CMD_VALUES})

# abstract values for all variables
list(GET _CMD_VALUES 0 PYTHON_VERSION)
list(GET _CMD_VALUES 1 PYTHON_PREFIX)
list(GET _CMD_VALUES 2 PYTHON_INCLUDE_PATH)
list(GET _CMD_VALUES 3 PYTHON_LIB_PATH)
list(GET _CMD_VALUES 4 PYTHON_EXT_SUFFIX)
list(GET _CMD_VALUES 5 PYTHON_SITE_PATH)

if(PYTHON_VERSION LESS 3.0)
    message(STATUS "default is python2, now try to find python3")
    
    execute_process( COMMAND "python3" "${PROJECT_SOURCE_DIR}/PyBind/PythonPath.py"
        RESULT_VARIABLE _CMD_SUCCESS
        OUTPUT_VARIABLE _CMD_VALUES
        ERROR_VARIABLE _CMD_ERROR   )

    if(NOT _CMD_SUCCESS MATCHES 0)
        message(FATAL_ERROR ${_CMD_ERROR})
        return()
    endif()

    # Convert the process output into a list
    string(REGEX REPLACE "\n" ";" _CMD_VALUES ${_CMD_VALUES})

    # abstract values for all variables
    list(GET _CMD_VALUES 0 PYTHON_VERSION)
    list(GET _CMD_VALUES 1 PYTHON_PREFIX)
    list(GET _CMD_VALUES 2 PYTHON_INCLUDE_PATH)
    list(GET _CMD_VALUES 3 PYTHON_LIB_PATH)
    list(GET _CMD_VALUES 4 PYTHON_EXT_SUFFIX)
    list(GET _CMD_VALUES 5 PYTHON_SITE_PATH)
endif()


if(CMAKE_HOST_WIN32)
    set(PYTHON_LIB_PATH "${PYTHON_PREFIX}/libs")
    set(PYTHON_LIB "python${PYTHON_VERSION}.lib")

    if(NOT EXISTS "${PYTHON_PREFIX}/libs/${PYTHON_LIB}")
        message(FATAL_ERROR "Python libraries not found: pythonXX.lib" )
    endif()
else()
    # for linux or posix, now there is no need to link python lib file
    set(PYTHON_LIB "")
endif()

#print Python Path information
message( STATUS "PYTHON_VERSION IS " ${PYTHON_VERSION} )
message( STATUS "PYTHON_PREFIX IS " ${PYTHON_PREFIX} )
message( STATUS "PYTHON_INCLUDE_PATH IS " ${PYTHON_INCLUDE_PATH} )
message( STATUS "PYTHON_LIB_PATH IS " ${PYTHON_LIB_PATH} )
message( STATUS "PYTHON_LIB IS " ${PYTHON_LIB} )
message( STATUS "PYTHON_EXT_SUFFIX IS " ${PYTHON_EXT_SUFFIX} )
message( STATUS "PYTHON_SITE_PATH IS " ${PYTHON_SITE_PATH} )
